#ifndef CUDPSENDTHREAD_HPP
#define CUDPSENDTHREAD_HPP

#include <semaphore.h>

#include "SimpleThread.h"
#include "UdpClient.hpp"

class CUdpSendThread : public SimpleThread {
 public:
  UdpClient udp;
  CUdpSendThread();
  void Stop();
  int Run();
  queue<Mat> mat_que;
  Sem mSemUdp;
  ~CUdpSendThread();

 private:
};

CUdpSendThread::CUdpSendThread() {}
CUdpSendThread::~CUdpSendThread() {}

void CUdpSendThread::Stop() {
  mShouldExit = true;
  SimpleThread::Stop();
}

int CUdpSendThread::Run() {
  static int frame_idx = 0;
  prctl(PR_SET_NAME, (unsigned long)"UdpSendThread");
  cpu_set_t mask;
  CPU_ZERO(&mask);
  CPU_SET(0, &mask);
  if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
    perror("pthread_setaffinity_np");
  }
  SUdpData udp_data;
  while (1) {
    mSemUdp.Wait();
    if (mat_que.size() > 0) {
      frame_idx++;
      udp_data.udp_head.mIndex = frame_idx;
      Mat tmp_mat = mat_que.front();
      mat_que.pop();
      udp_data.src = tmp_mat;
      udp.UdpMatSender(udp_data);
    }
  }
  return 0;
}

#endif
